<?php
namespace app\api\controller;

use app\api\Base;
use think\Config;
use think\Db;
use com\gdl\newagk\interfaces\JpushServiceClient;
use com\gdl\newagk\dataobject\jpush\JpushRequestDTO; 

/**
 * 极光推送相关接口
 * @author tc 2018/04/16
 */

class Jpush extends Base
{
 	/** 
 	 *  单条消息推送通知应用后台
 	 *  @return JSON [description] 400失败 200成功  
 	 */
	public function jpushUserNotice() 
    {
		header('Access-Control-Allow-Origin:*');
        // 响应类型  
        header('Access-Control-Allow-Methods:POST');
        // 响应头设置  
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
    	$msg = ['code'=>400, 'msg'=>'推送失败', 'data'=>[]]; 
        $id = $this->request->param('id') ? $this->request->param('id') : "";
        $jpushcount = $this->request->param('jpushcount') ? $this->request->param('jpushcount') : "";
        $content = $this->request->param('content') ? getStr($this->request->param('content')) : ""; 
		$pushdata = $this->request->param('pushdata') ? getStr($this->request->param('pushdata')) : ""; 

        try { 
       
		   \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlAgkSdk','gdlApiSdk')); 
			#接口
            $JpushService = new JpushServiceClient(NULL);
           
            \ApiClient::build($JpushService);
            # 数据
            $jpushDo = new JpushRequestDTO();
            $jpushDo->id = $id;
            $jpushDo->jpushcount = $jpushcount;
            $jpushDo->content = $content;
            $jpushDo->pushdata = $pushdata;
var_dump($JpushService);die;
            $result = $JpushService->jpushUserNotice($jpushDo); 
            if ($result->success == TRUE) {
            	$msg['msg'] = $result->message;
                $msg['code'] = $result->code;
                $msg['data'] = $result->data;
                if ($result->code == 200) {
                    $msg['code'] = $result->code;
                }

            } 

        } catch (Exception $ex) {
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    }
    /**
     * 通过alias 获取唯一标识
     * 考试违规通知指定用户
     */
    public function jpushUserPaper(){
        $msg = ['code'=>400, 'msg'=>'推送失败', 'data'=>[]]; 
        $id = $this->request->param('id') ? $this->request->param('id') : "";
        $jpushcount = $this->request->param('jpushcount') ? $this->request->param('jpushcount') : "";
        $content = $this->request->param('content') ? getStr($this->request->param('content')) : ""; 
        $pushdata = $this->request->param('pushdata') ? getStr($this->request->param('pushdata')) : ""; 
         try { 
       
           \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlAgkSdk','gdlApiSdk')); 
            #接口
            $JpushService = new JpushServiceClient(NULL);
           
            \ApiClient::build($JpushService);
            # 数据
            $jpushDo = new JpushRequestDTO();
            $jpushDo->id = $id;
            $jpushDo->jpushcount = $jpushcount;
            $jpushDo->content = $content;
            $jpushDo->pushdata = $pushdata;

            $result = $JpushService->jpushUserPaper($jpushDo);  
            if ($result->success == TRUE) {
                $msg['msg'] = $result->message;
                $msg['code'] = $result->code;
                $msg['data'] = $result->data;
                if ($result->code == 200) {
                    $msg['code'] = $result->code;
                }

            } 

        } catch (Exception $ex) {
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    }
    /*测试接口*/
    public function jpushUserCustom(){
         $msg = ['code'=>400, 'msg'=>'推送失败', 'data'=>[]]; 
        $id = $this->request->param('id') ? $this->request->param('id') : "";
        $jpushcount = $this->request->param('jpushcount') ? $this->request->param('jpushcount') : "";
        $content = $this->request->param('content') ? getStr($this->request->param('content')) : ""; 
        $pushdata = $this->request->param('pushdata') ? getStr($this->request->param('pushdata')) : ""; 
         try { 
       
           \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlAgkSdk','gdlApiSdk')); 
            #接口
            $JpushService = new JpushServiceClient(NULL); 
            \ApiClient::build($JpushService);
            # 数据
            $jpushDo = new JpushRequestDTO();
            $jpushDo->id = $id;
            $jpushDo->jpushcount = $jpushcount;
            $jpushDo->content = $content;
            $jpushDo->pushdata = $pushdata; 
            // var_dump($JpushService);die;
            $result = $JpushService->jpushUserCustom($jpushDo);  
            var_dump($result);die;
            if ($result->success == TRUE) {
                $msg['msg'] = $result->message;
                $msg['code'] = $result->code;
                $msg['data'] = $result->data;
                if ($result->code == 200) {
                    $msg['code'] = $result->code;
                }

            } 

        } catch (Exception $ex) {
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    } 
    
}
